package com.example.rab.controller;

import com.example.rab.annotation.PermissionAnnotation;
import com.example.rab.entity.domain.User;
import com.example.rab.service.UserService;
import com.example.rab.utils.JwtUtils;
import com.example.rab.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@RestController
@RequestMapping("/wx")
@Slf4j
public class UserController {

//    @Resource(name = "directConnectionESClient")
//    ElasticsearchClient client
//    @PermissionAnnotation环绕方法测试

    @Resource
    AuthenticationManager authenticationManager;

    @Resource
    UserService userService;

    @Resource
    PasswordEncoder passwordEncoder;

    @PermissionAnnotation
    @PostMapping("/login")
    public R login(@RequestBody Map<String, Object> map) {
        String phone = map.get("phone").toString();
        String password = map.get("password").toString();
        try {
            UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(phone, password);
            Authentication authentication = authenticationManager.authenticate(auth);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            UserDetails userDetails = (UserDetails) authentication.getPrincipal();
            String jwtToken = JwtUtils.getToken(phone);
            Map<String,Object>dat=new HashMap<>();
            dat.put("token",jwtToken);
            dat.put("user",userDetails);
            return R.success(dat);
        } catch (Exception e) {
            log.error("登录失败");
            return R.error("登录失败");
        }
    }

    @PermissionAnnotation
    @PostMapping("/register")
    public R register(@RequestBody User user) {
        try {
            if (user.getPassword() != null && !user.getPassword().isEmpty()) {
                String password = passwordEncoder.encode(user.getPassword());
                user.setPassword(password);
                if (userService.insertUser(user) == null) {
                    throw new Exception("用户名已存在");
                }
                String jwtToken = JwtUtils.getToken(user.getPhone());
                Map<String,Object>dat=new HashMap<>();
                dat.put("token",jwtToken);
                dat.put("user",user);
                return R.success(dat);
            } else {
                throw new Exception("密码为空");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error("注册失败" + e.getMessage());
        }
    }
}
